//
//  CallUserManger.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/3/26.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "CallUserManger.h"
#import "UserInfoLogic.h"
#import "CallUserViewController.h" //呼叫页面
#import "VMsgMessage.h"
#import "MSGOnSender.h"
#import "RechargeViewController.h"
@interface CallUserManger()<UserInfoLogicDelegate>
{
    UserInfoLogic *_logic;
    UIViewController *_vc;
    int _status;
}
@end

@implementation CallUserManger
SINGLETON_FOR_CLASS(CallUserManger)
-(void)CallUserWithUID:(NSString *)uid AndVC:(UIViewController *)vc
{
    if(!StrValid(uid))
    {
        return;
    }
    
    if(vc == nil)
    {
        return;
    }
    
    _status = 0;
    _vc = vc;
    _logic = [UserInfoLogic new];
    _logic.uid = uid;
    _logic.delegagte = self;
    
    [[HUDHelper sharedInstance] syncLoading];
    
    //防止请求用户失败一直不消失菊花
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if(_status == 0)
        {
            [[HUDHelper sharedInstance] syncStopLoading];
        }
    });
    
    [_logic loadData];
}


-(void)CallUserV2WithUid:(NSString *)uid ViewControler:(UIViewController *)vc
{
    
    if(!StrValid(uid))
    {
        return;
    }
    
    if(vc == nil)
    {
        return;
    }
    _vc = vc;
    _status = 0;
    _vc = vc;
    _logic = [UserInfoLogic new];
    _logic.uid = uid;
    _logic.delegagte = self;
    
    [[HUDHelper sharedInstance] syncLoading];
    
    //防止请求用户失败一直不消失菊花
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if(_status == 0)
        {
            [[HUDHelper sharedInstance] syncStopLoading];
        }
    });
    
    [_logic loadDataNoCache];
}
#pragma mark - 数据加载完成回调
-(void)requestDataCompleted
{

    
    [[HUDHelper sharedInstance] syncLoading];
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"video_call_api" A:@"video_call_0907"];
    //
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:_logic.model.id forKey:@"id"];
    
    [CYNET POSTv3:url parameters:param responseCache:^(id responseObject) {
        
    } success:^(id responseObject) {
        NSDictionary *data = [responseObject valueForKey:@"data"];
        _status = 1;
        [[HUDHelper sharedInstance] syncStopLoading];
        NSString *channel_id = [data valueForKey:@"channel_id"];
        Sender *sender = [Sender new];
        sender.id = curUser.id;
        sender.avatar = curUser.avatar;
        sender.user_nickname = curUser.user_nickname;
        
        VMsgMessage *vmessage = [[VMsgMessage alloc] init];
        vmessage.channel = channel_id;
        vmessage.sender = sender;
        vmessage.type = ReceiveCall;
        vmessage.reply_type = 2;
        [[MSGOnSender new] pushCallMessage:vmessage Uid:_logic.model.id];
        
        [[HUDHelper sharedInstance] syncStopLoading];
        //跳转到视频界面
        CallUserViewController *callView = [CallUserViewController new];
        callView.usermodel = _logic.model;
        callView.channel = [data valueForKey:@"channel_id"];
        callView.iscall = YES;
        [_vc.navigationController pushViewController:callView animated:NO];
    } failure:^(NSString *error, NSInteger code) {
        [[HUDHelper sharedInstance] syncStopLoading];
        if(code == 10002)
        {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"当前余额不足，是否前往充值" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                RechargeViewController *vc = [RechargeViewController new];
                [kAppDelegate pushViewController:vc];
                    
                NSLog(@"确定按键被点击");
            }];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                NSLog(@"取消按键被点击");
            }];
            
            [alert addAction:sureAction];
            [alert addAction:cancelAction];
            [_vc presentViewController:alert animated:YES completion:^{
            }];
        }
        else
        {
            [MBProgressHUD showTopTipMessage:error];
        }
    } hasCache:NO];
    
    
    
////    [[HUDHelper sharedInstance] syncLoading];
//    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"video_call_api" A:@"video_call"];
//    NSMutableDictionary *param = [NSMutableDictionary dictionary];
//    [param setObject:_logic.uid forKey:@"id"];
//    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
//
//    } success:^(id responseObject) {
//
//
//
//
//    } failure:^(NSString *error) {
//        [[HUDHelper sharedInstance] syncStopLoading];
//        [MBProgressHUD showTopTipMessage:error];
//    } hasCache:NO];
    
}

-(void)CallRemoveMessage:(NSDictionary *)message
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(ASLocalizedString(@"当前状态%@"),KGlobalVariable.CallState);
        if(![KGlobalVariable.CallState isEqualToString:@"1"])
        {
            VMsgMessage *model = [VMsgMessage modelWithJSON:[message valueForKey:@"custom_data"]];
            NSLog(@"cccc %@",model.channel);
        
        
        CallUserViewController *call = [CallUserViewController new];
        call.msessage = model;
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:call];
        [[kAppDelegate getCurrentVC] presentViewController:nav animated:NO completion:nil];
        
        }
    });
}
@end
